#pragma once

#include <iostream>
#include <assert.h>
#include <string.h>


using namespace std;

namespace wlc
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;


		//template <typename InputIteratoe>
		//vector(InputIteratoe first, InputIteratoe last)
		//{
		//	while (first != last)
		//	{
		//		push_back(*first);
		//		first++;
		//	}
		//}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstorage, v._endofstorage);
		}

		vector<T>& operator=(vector<T>
			v)
		{
			swap(v);
			return *this;
		}



		vector(iterator start, iterator end)
		{
			while (start != end)
			{
				push_back(*start);
				start++;
			}
		}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto ch : il)
			{
				push_back(ch);
			}
		}

		vector(size_t n, T val = T())
		{
			reserve(n);
			while (n--)
			{
				push_back(val);
			}
		}

		vector(const vector<T>& v)
			: _start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			reserve(v.capacity());
			memcpy(_start, v._start, sizeof(T) * v.size());
			_finish = _start + v.size();
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstorage = nullptr;
			}
		}

		iterator begin()
		{
			return _start;
		}

		const_iterator begin()const
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return (_finish - _start);
		}

		size_t capacity() const
		{
			return (_endofstorage - _start);
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old_size = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < old_size; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old_size;
				_endofstorage = _start + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _endofstorage)
			{
				size_t new_capacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(new_capacity);
			}
			*_finish = x;
			_finish++;
		}

		void Pop_back()
		{
			assert(!empty());
			_finish--;
			
		}

		bool empty() const
		{
			return _start == _finish;
		}
		
		T& operator[](size_t n)
		{
			assert(n < size());
			return *(_start + n);
		}

		const T& operator[](size_t n)const
		{
			assert(n < size());
			return *(_start + n);
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				int new_capacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(new_capacity);
				pos = len + _start;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				end--;
			}
			*pos = x;
			_finish++;

			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator it = pos + 1;
			while (it != _finish)
			{
				*(it - 1) = *it;
				it++;
			}
			_finish--;

			return pos;
		}

		void clear()
		{
			_finish = _start;
		}

		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

	


	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

}


